<?php

namespace App\Services\Assess;

use App\Helpers\JwtUtils;
use App\Models\Assess\BuyerModel;
use App\Services\BaseService;

class BuyerService extends BaseService
{

    //激活失败展示邮箱
    public $activateDispalyEmail = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //复购仅激活
    public $onlyActivate = [
        ['label' => '开启', 'val' => 1],
        ['label' => '关闭', 'val' => 2]
    ];
    //显示用户走留评环节
    public $displayCommentLink = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //是否展示礼品选择页面
    public $displayGiftSelect = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //过滤折扣订单
    public $filterDiscountOrders = [
        ['label' => '不过滤', 'val' => 1],
        ['label' => '所有折扣单', 'val' => 2],
        ['label' => '指定范围折扣', 'val' => 3],
        ['label' => '指定阶段折扣', 'val' => 4]
    ];
    //仅激活一次
    public $activateOnlyOnce = [
        ['label' => '开启', 'val' => 1],
        ['label' => '关闭', 'val' => 2]
    ];
    //星级
    public $starRatingRetention = [
        ['label' => '1星', 'val' => 1],
        ['label' => '2星', 'val' => 2],
        ['label' => '3星', 'val' => 3],
        ['label' => '4星', 'val' => 4],
        ['label' => '5星', 'val' => 5],
    ];
    //手机号必填
    public $phoneRequire = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //手机号展示
    public $phoneShow = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //自动加入评论监控
    public $autoCommentMonitor = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //地址展示
    public $addressShow = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //地址默认数据清空
    public $addressDefaultClear = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];
    //敏感词不引导留评
    public $sensitiveWordNo = [
        ['label' => '开启', 'val' => 1],
        ['label' => '关闭', 'val' => 2]
    ];
    //同一ip日弹窗1次
    public $ipPop = [
        ['label' => '是', 'val' => 1],
        ['label' => '否', 'val' => 2]
    ];


    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new BuyerModel();
    }

    /**
     * @Notes:获取买家端设置
     * @Interface getGiftConfig
     * @author: wuyu
     * @Time: 2023/4/24   12:03`
     */
    public function getBuyerConfig()
    {
        $config = [];
        $config['activate_dispaly_email'] = $this->activateDispalyEmail;
        $config['only_activate'] = $this->onlyActivate;
        $config['display_comment_link'] = $this->displayCommentLink;
        $config['display_gift_select'] = $this->displayGiftSelect;
        $config['filter_discount_orders'] = $this->filterDiscountOrders;
        $config['activate_only_once'] = $this->activateOnlyOnce;
        $config['star_rating'] = $this->starRatingRetention;
        $config['phone_require'] = $this->phoneRequire;
        $config['phone_show'] = $this->phoneShow;
        $config['auto_comment_monitor'] = $this->autoCommentMonitor;
        $config['address_show'] = $this->addressShow;
        $config['address_default_clear'] = $this->addressDefaultClear;
        $config['sensitive_word_no'] = $this->sensitiveWordNo;
        $config['ip_pop'] = $this->ipPop;

        return $config;
    }


    /**
     * @Notes:获取买家端数据
     * @Interface getBuyerData
     * @author: wuyu
     * @Time: 2023/4/25   14:11
     */
    public function getBuyerData()
    {

        $buyerList = $this->model->getOne();
        $buyerList['register_success_page_script'] = !empty($buyerList['register_success_page_script']) ? json_decode($buyerList['register_success_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['register_no_page_script'] = !empty($buyerList['register_no_page_script']) ? json_decode($buyerList['register_no_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['gift_select_page_script'] = !empty($buyerList['gift_select_page_script']) ? json_decode($buyerList['gift_select_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['comment_page_script'] = !empty($buyerList['comment_page_script']) ? json_decode($buyerList['comment_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['amazon_comment_page_script'] = !empty($buyerList['amazon_comment_page_script']) ? json_decode($buyerList['amazon_comment_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['gift_product_page_script'] = !empty($buyerList['gift_product_page_script']) ? json_decode($buyerList['gift_product_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['gift_card_page_script'] = !empty($buyerList['gift_card_page_script']) ? json_decode($buyerList['gift_card_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['gift_cashback_page_script'] = !empty($buyerList['gift_cashback_page_script']) ? json_decode($buyerList['gift_cashback_page_script'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['gift_end_page_script'] = !empty($buyerList['gift_end_page_script']) ? json_decode($buyerList['gift_end_page_script'], true) : json_decode('{"script_1": ""}', true);

        $buyerList['register_page_script'] = json_decode($buyerList['register_page_script'],true);

        $buyerList['register_page_script_ger'] = !empty($buyerList['register_page_script']['register_page_script_ger']) ? json_decode($buyerList['register_page_script']['register_page_script_ger'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['register_page_script_it'] = !empty($buyerList['register_page_script']['register_page_script_it']) ? json_decode($buyerList['register_page_script']['register_page_script_it'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['register_page_script_sp'] = !empty($buyerList['register_page_script']['register_page_script_sp']) ? json_decode($buyerList['register_page_script']['register_page_script_sp'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['register_page_script_jp'] = !empty($buyerList['register_page_script']['register_page_script_jp']) ? json_decode($buyerList['register_page_script']['register_page_script_jp'], true) : json_decode('{"script_1": ""}', true);
        $buyerList['register_page_script'] = !empty($buyerList['register_page_script']['register_page_script']) ? json_decode($buyerList['register_page_script']['register_page_script'], true) : json_decode('{"script_1": ""}', true);

        return $buyerList;
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function edit()
    {
        try {
            $params = request()->all();
            $data = [
                'contact_email' => $params['contact_email'] ?? '',
                'activate_dispaly_email' => $params['activate_dispaly_email'] ?? 0,
                'only_activate' => $params['only_activate'] ?? 0,
                'filter_discount_orders' => $params['filter_discount_orders'] ?? 0,
                'activate_only_once' => $params['activate_only_once'] ?? 0,
                'star_rating' => $params['star_rating'] ?? 0,
                'comment_mini_char' => $params['comment_mini_char'] ?? 10,
                'phone_require' => $params['phone_require'] ?? 0,
                'phone_show' => $params['phone_show'] ?? 0,
                'address_show' => $params['address_show'] ?? 0,
                'address_default_clear' => $params['address_default_clear'] ?? 0,
                'comment_sensitive_word' => $params['comment_sensitive_word'] ?? 0,
                'sensitive_word_no' => $params['sensitive_word_no'] ?? 0,
                'ip_pop' => $params['ip_pop'] ?? 0,
                'jump_second' => $params['jump_second'] ?? 0,
                'email_show' => $params['email_show'] ?? 0,
                'display_comment_link' => $params['display_comment_link'] ?? 0,
                'auto_comment_monitor' => $params['auto_comment_monitor'] ?? 0,
                'find_my_order' => $params['find_my_order'] ?? '',
                'comment_platform_sku' => $params['comment_platform_sku'] ?? '',
                'display_gift_select' => $params['display_gift_select'] ?? 0,
                'comment_gift_platform_sku' => $params['comment_gift_platform_sku'] ?? '',
            ];

            $data['comment_platform_sku'] = str_replace(['；', ',', '，',"\r\n", "\r", "\n"], ';', $data['comment_platform_sku']);
            $data['comment_gift_platform_sku'] = str_replace(['；', ',', '，',"\r\n", "\r", "\n"], ';', $data['comment_gift_platform_sku']);

            if ($data['display_comment_link'] ==2 && empty($data['comment_platform_sku'])){
                throw new \Exception('不展示走留评界面;请填写相应asin');
            }
            if ($data['display_gift_select'] ==2 && empty($data['comment_gift_platform_sku'])){
                throw new \Exception('不展示礼品选择页面;请填写相应asin');
            }

            //多语言
            if (!empty($params['register_page_script'])) $data['register_page_script']['register_page_script'] = json_encode($params['register_page_script']);
            if (!empty($params['register_page_script_ger'])) $data['register_page_script']['register_page_script_ger'] = json_encode($params['register_page_script_ger']);
            if (!empty($params['register_page_script_it'])) $data['register_page_script']['register_page_script_it'] = json_encode($params['register_page_script_it']);
            if (!empty($params['register_page_script_sp'])) $data['register_page_script']['register_page_script_sp'] = json_encode($params['register_page_script_sp']);
            if (!empty($params['register_page_script_jp'])) $data['register_page_script']['register_page_script_jp'] = json_encode($params['register_page_script_jp']);
            $data['register_page_script'] = json_encode($data['register_page_script']);

            if (!empty($params['register_success_page_script'])) $data['register_success_page_script'] = json_encode($params['register_success_page_script']);
            if (!empty($params['register_no_page_script'])) $data['register_no_page_script'] = json_encode($params['register_no_page_script']);
            if (!empty($params['gift_select_page_script'])) $data['gift_select_page_script'] = json_encode($params['gift_select_page_script']);
            if (!empty($params['comment_page_script'])) $data['comment_page_script'] = json_encode($params['comment_page_script']);
            if (!empty($params['amazon_comment_page_script'])) $data['amazon_comment_page_script'] = json_encode($params['amazon_comment_page_script']);
            if (!empty($params['gift_product_page_script'])) $data['gift_product_page_script'] = json_encode($params['gift_product_page_script']);
            if (!empty($params['gift_card_page_script'])) $data['gift_card_page_script'] = json_encode($params['gift_card_page_script']);
            if (!empty($params['gift_cashback_page_script'])) $data['gift_cashback_page_script'] = json_encode($params['gift_cashback_page_script']);
            if (!empty($params['gift_end_page_script'])) $data['gift_end_page_script'] = json_encode($params['gift_end_page_script']);

            // 开启事务
            $userId = JwtUtils::getUserId();
            if (!empty($params['id'])) {
                $id = $params['id'];
                $data['id'] = $id;
                $data['update_time'] = time();
                $this->model->where('id', $id)->update($data);
                $this->model->cacheReset($id, $data, $id);
            } else {
                $data['create_user'] = $data['update_user'] = $userId;
                $data['create_time'] = time();
                $id = $this->model->insertGetId($data);
            }
            if (!$id) {
                throw new \Exception('保存失败');
            }

            return message("保存成功", true, []);

        } catch (\Exception $e) {
            return message("保存失败:" . $e->getMessage(), false, []);
        }

    }

}
